/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SearchResultView.java 2008-11-11
 */
package ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane;

import ie.ucc.cs.anggao.finalproject.client.model.SearchEngine;
import ie.ucc.cs.anggao.finalproject.client.model.SearchEngineResult;
import ie.ucc.cs.anggao.finalproject.client.support.css.CSSManager;
import ie.ucc.cs.anggao.finalproject.client.support.searchengine.ResultLink;
import ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane.PageableFlextTable.PageableFlexTable;

import java.util.ArrayList;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author GaoAng
 * 
 */
public class CopyOfSearchResultView extends Composite {

	private DisclosurePanel mainPanel = null;
	// private VerticalPanel results = new VerticalPanel();

	private PageableFlexTable results;
	private SearchEngine engine;
	private Object[][] data;

	public CopyOfSearchResultView(SearchEngine engine) {
		this.engine = engine;
		mainPanel = new DisclosurePanel(engine.getName());
		mainPanel.setContent(results);
		initWidget(mainPanel);
		mainPanel.setStyleName(CSSManager.SearchResultsView);

	}

	public void clearResults() {
		results.clear();
		mainPanel.setOpen(true);
	}

	public void addSearchResult(ArrayList<SearchEngineResult> result) {

		data = new Object[result.size()][0];
		Object[] data1 = new Object[result.size()];
		int i =0;
		for (final SearchEngineResult SEresult : result) {

			VerticalPanel resultItem = new VerticalPanel();
			ResultLink title = new ResultLink(SEresult.getTitle(), SEresult
					.getUrl());
			HTML description = new HTML(SEresult.getDescription());

			description.addClickListener(new ClickListener() {
				public void onClick(Widget w) {
					PopupPanel p = new PopupPanel(true);
					//p.addStyleName("keepPopup");
					p.setWidget(getImage(SEresult.getUrl()));
					p.setPopupPosition(w.getAbsoluteLeft() + 5, w
							.getAbsoluteTop());
					p.show();
				}
			});

			resultItem.add(title);
			resultItem.add(description);
			resultItem.addStyleName(CSSManager.ResultItem);
			title.setStyleName(CSSManager.searchResultLink);
			description.setStyleName(CSSManager.searchResultDescription);

			data1[i] = resultItem;
			i++;

		}

		
		for (i = 0; i < data1.length; i++) {

			data[i][0] = data1[i];
		}
		results = new PageableFlexTable(10) {
			public Object[][] getData(int index) {
				int pageSize = getPageSize();

				if (isLastPage(index))
					index = data.length - pageSize;

				Object[][] array = new Object[pageSize][];
				for (int row = index; row < index + pageSize; ++row) {
					array[row - index] = data[row];
				}
				return array;
			}

			public boolean isLastPage(int topOfPageIndex) {
				return topOfPageIndex + getPageSize() >= data.length;
			}
		};
		
		mainPanel.add(results);
		initWidget(mainPanel);

		/*
		 * VerticalPanel resultItem = new VerticalPanel(); ResultLink title =
		 * new ResultLink(result.getTitle(), result.getUrl()); HTML description
		 * = new HTML(result.getDescription());
		 * 
		 * description.addClickListener(new ClickListener() { public void
		 * onClick(Widget w) { PopupPanel p = new PopupPanel(true); //
		 * p.addStyleName("keepPopup"); p.setWidget(getImage(result.getUrl()));
		 * p.setPopupPosition(w.getAbsoluteLeft() + 5, w.getAbsoluteTop());
		 * p.show(); } });
		 * 
		 * resultItem.add(title); resultItem.add(description);
		 * resultItem.addStyleName(CSSManager.ResultItem);
		 * title.setStyleName(CSSManager.searchResultLink);
		 * description.setStyleName(CSSManager.searchResultDescription);
		 * 
		 * results.add(resultItem);
		 */

	}

	public Widget getImage(String url) {

		Image image = new Image();
		image.setUrl("http://open.thumbshots.org/image.aspx?url=" + url);
		return image;

	}

	public SearchEngine getSearchEngine() {
		return this.engine;
	}

}
